<template>
  <a-modal
    :open="open"
    width="960px"
    title="手术预约"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    :confirm-loading="confirmLoading"
    @cancel="onCancel"
    @ok="onOk"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="手术名称" v-bind="validateInfos.oprCode" class="w-2/3">
        <a-input v-if="NameEleMode" v-model:value="formState.oprCode" placeholder="请输入" />
        <SelectTable
          v-else
          v-model:value="formState.oprName"
          type="手术icd9"
          @select=";(formState.oprCode = $event.icd9Code), (formState.oprName = $event.icd9Name)"
        />
      </a-form-item>
      <a-form-item label="手术分类" class="w-1/3" v-bind="validateInfos.oprTypeCode">
        <SelectItem v-model:value="formState.oprTypeCode" type="DT00037" placeholder="请选择" allow-clear />
      </a-form-item>
      <a-form-item label="部位" class="w-1/3">
        <!-- <a-select v-model:value="formState.oprAptmPartList" placeholder="请选择" mode="multiple" :max-tag-count="1" :options="positionList" allow-clear /> -->
        <SelectPartTags
          :value="{ partCode: formState.oprAptmPartList, partName: formState.oprAptmParts }"
          multiple
          allow-clear
          show-new
          :input-or-textarea="true"
          type="部位"
          @select=";(formState.oprAptmPartList = $event?.partCode), (formState.oprAptmParts = $event?.partName)"
        />
      </a-form-item>
      <a-form-item label="麻醉方式" class="w-1/3">
        <SelectItem v-model:value="formState.anaesCode" type="DT00012" placeholder="请选择" allow-clear />
      </a-form-item>
      <a-form-item label="切口等级" class="w-1/3">
        <SelectItem v-model:value="formState.cutLevelCode" type="DT00035" placeholder="请选择" allow-clear />
      </a-form-item>
      <a-form-item label="愈合类别" class="w-1/3">
        <SelectItem v-model:value="formState.healingCategoryCode" type="heal_lv_code" placeholder="请选择" allow-clear />
      </a-form-item>
      <a-form-item label="拟手术日期" v-bind="validateInfos.exptOprDate" class="w-1/3">
        <a-date-picker v-model:value="formState.exptOprDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="手术入路" class="w-1/3">
        <SelectItem v-model:value="formState.approachCode" type="DT00036" placeholder="请选择" allow-clear />
      </a-form-item>
      <a-form-item label="手术等级" class="w-1/3" v-bind="validateInfos.oprLevelCode">
        <SelectItem v-model:value="formState.oprLevelCode" type="DT00034" placeholder="请选择" allow-clear />
      </a-form-item>
      <!-- v-bind="validateInfos.oprDoctorName" -->
      <a-form-item label="手术医生" class="w-1/3">
        <SelectTable
          v-model:value="formState.oprDoctorName"
          type="医生"
          @select=";(formState.oprDoctor = $event.empId), (formState.oprDoctorName = $event.empName)"
        />
      </a-form-item>
      <a-form-item label="麻醉医生" class="w-1/3">
        <SelectTable
          v-model:value="formState.anaesDoctorName"
          type="医生"
          @select=";(formState.anaesDoctor = $event.empId), (formState.anaesDoctorName = $event.empName)"
        />
      </a-form-item>
      <a-form-item class="w-full ml-[100px]">
        <a-checkbox v-model:checked="formState.isEcg">心电监护</a-checkbox>
        <a-checkbox v-model:checked="formState.isRecurrence">复发</a-checkbox>
      </a-form-item>
      <a-form-item label="联合手术1" class="w-1/2">
        <a-input v-if="NameEleMode" v-model:value="formState.jointOprCode1" placeholder="请输入" />
        <SelectTable
          v-else
          v-model:value="formState.jointOprName1"
          type="手术icd9"
          @select=";(formState.jointOprCode1 = $event.icd9Code), (formState.jointOprName1 = $event.icd9Name)"
        />
      </a-form-item>
      <a-form-item label="联合手术2" class="w-1/2">
        <a-input v-if="NameEleMode" v-model:value="formState.jointOprCode2" placeholder="请输入" />
        <SelectTable
          v-else
          v-model:value="formState.jointOprName2"
          type="手术icd9"
          @select=";(formState.jointOprCode2 = $event.icd9Code), (formState.jointOprName2 = $event.icd9Name)"
        />
      </a-form-item>
      <a-form-item label="联合手术3" class="w-1/2">
        <a-input v-if="NameEleMode" v-model:value="formState.jointOprCode3" placeholder="请输入" />
        <SelectTable
          v-else
          v-model:value="formState.jointOprName3"
          type="手术icd9"
          @select=";(formState.jointOprCode3 = $event.icd9Code), (formState.jointOprName3 = $event.icd9Name)"
        />
      </a-form-item>
      <a-form-item label="联合手术4" class="w-1/2">
        <a-input v-if="NameEleMode" v-model:value="formState.jointOprCode4" placeholder="请输入" />
        <SelectTable
          v-else
          v-model:value="formState.jointOprName4"
          type="手术icd9"
          @select=";(formState.jointOprCode4 = $event.icd9Code), (formState.jointOprName4 = $event.icd9Name)"
        />
      </a-form-item>
      <a-form-item label="联合手术5" class="w-1/2">
        <a-input v-if="NameEleMode" v-model:value="formState.jointOprCode5" placeholder="请输入" />
        <SelectTable
          v-else
          v-model:value="formState.jointOprName5"
          type="手术icd9"
          @select=";(formState.jointOprCode5 = $event.icd9Code), (formState.jointOprName5 = $event.icd9Name)"
        />
      </a-form-item>
      <a-form-item label="联合手术6" class="w-1/2">
        <a-input v-if="NameEleMode" v-model:value="formState.jointOprCode6" placeholder="请输入" />
        <SelectTable
          v-else
          v-model:value="formState.jointOprName6"
          type="手术icd9"
          @select=";(formState.jointOprCode6 = $event.icd9Code), (formState.jointOprName6 = $event.icd9Name)"
        />
      </a-form-item>
      <a-form-item label="特殊要求" class="w-full">
        <a-input v-model:value="formState.specRequirements" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="嘱托" class="w-full">
        <a-textarea v-model:value="formState.advMemo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import SelectTable from '@/components/SelectTable'
import { post } from '@/utils/request'
import { getSystemConfig } from '@/utils/systemConfig'
import SelectItem from '@/components/SelectItem'
import useStore from '../../store'

const props = defineProps({
  open: Boolean,
  isOutDoctor: {
    //门诊或住院   true 门诊  false 住院
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  },
  jzbh: {
    type: String,
    default: ''
  }
})
const store = useStore()
const NameEleMode = ref(true)
const operationClassify = ref('')
const formState = ref({
  oprCode: '',
  exptOprDate: '',
  oprLevelCode: '',
  // oprDoctorName: '',
  oprTypeCode: ''
})
const emit = defineEmits(['update:open', 'success'])
const rulesState = ref({
  oprCode: [{ required: true, message: '请选择手术' }],
  exptOprDate: [{ required: true, message: '请选择拟手术日期' }],
  oprLevelCode: [{ required: true, message: '请选择手术登记' }],
  // oprDoctorName: [{ required: true, message: '请选择手术医生' }],
  oprTypeCode: [{ required: true, message: '请选择手术分类' }]
})
const { resetFields, isCompleted, validateInfos } = useForm(formState, rulesState)

const confirmLoading = ref(false)
//保存手术
const onOk = async () => {
  formState.value.isEcg = formState.value.isEcg ? 1 : 0
  formState.value.isRecurrence = formState.value.isRecurrence ? 1 : 0
  formState.value.oprDate = formState.value.exptOprDate
  confirmLoading.value = true
  const res = await post('/oprOperation/opr/doSaveOperationRecordInfo', {
    keyLsh: props.jzbh || store.currentPatient.inplsh,
    isInpAdv: props.isOutDoctor ? '0' : '1',
    ...formState.value,
    oprPath: props.isOutDoctor ? '0' : '1' // 0是门诊，1是住院
  })
  confirmLoading.value = false
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  emit('success')
  onCancel()
}
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}

watch(
  () => props.open,
  () => {
    formState.value = { ...props.rowData, oprAptmPartList: props.rowData.oprAptmPartList?.length ? props.rowData.oprAptmPartList : [] }
    if (!formState.value.oprTypeCode && !formState.value.oprLsh) {
      formState.value.oprTypeCode = operationClassify.value
    }
    if (!formState.value.oprLsh) {
      formState.value.exptOprDate = dayjs(new Date().setDate(new Date().getDate() + 1)).format('YYYY-MM-DD')
    }
  },
  { immediate: true }
)

onMounted(() => {
  NameEleMode.value = getSystemConfig('OPERATION_APPLY_INPUT_MODE') === '0' //手术申请名称输入模式 0：手工输入模式|1：ICD9选择模式
  operationClassify.value = getSystemConfig('INP_OPR_DEFAULT_TYPE_CODE') //手术预约分类默认代码
})
</script>
<style lang="less" scoped></style>
